import time

import pandas as pd
import requests
import json

from bqtool.utils.mykit import functime


class EsClientHw:
    def __init__(self, baseurl):
        self.baseurl = baseurl

    @functime
    def queryByTime(self, index, type, time, startTime, endTime,mapStr):
        payload = {"index": index, "type": type, "time": time, "startTime": startTime,
                   "endTime": endTime,"mapStr":mapStr}
        r = requests.post(url=f'{self.baseurl}/query/scroll',data=payload)
        # payload = {"index": "koms_index_aibox_activeroad_tgs", "type": "koms_aibox_activeroad_tgs"}
        # r = requests.post(url=f'{self.baseurl}/query/findAll', data=payload)
        df = pd.DataFrame()
        if r.text != "" and r.text!="[]":
            # print(r.text)
            df = pd.DataFrame(r.json())
        return df

    def save(self, index, type, df):
        jsonData = df.to_json(orient="records")
        payload = {"jsonData": jsonData, "index": index, "type": type}
        r = requests.post(url=f'{self.baseurl}/query/save', data=payload)
        return r.text

    @functime
    def queryByTimes(self, index, type, time, startTime, endTime,mapStr):
        payload = {"index": index, "type": type, "time": time, "startTime": startTime,
                   "endTime": endTime,"mapStr":mapStr}
        r = requests.post(url=f'{self.baseurl}/query/scrollFirst',data=payload)
        df = pd.DataFrame()
        js = {}
        if r.text != "" and r.text!="[]":
            js = r.json()
            df = pd.concat([df,pd.DataFrame(js[0])])
        while(js[0]):
            r = requests.post(url=f'{self.baseurl}/query/scrollById',data={"scrollId":js[1]})
            js[0] = {}
            if r.text != "" and r.text!="[]":
                js = r.json()
                df = pd.concat([df, pd.DataFrame(js[0])])
        return df

    def delete(self, index, type, time, startTime, endTime,mapStr):
        payload = {"index": index, "type": type, "time": time, "startTime": startTime,
                   "endTime": endTime,"mapStr":mapStr}
        r = requests.post(url=f'{self.baseurl}/query/del',data=payload)
        return r.text

if __name__ == '__main__':
    # f = open(r'a.csv', encoding="gbk")
    # df = pd.read_csv(f)
    # client = EsClientHw("http://10.2.111.241:8081")
    client = EsClientHw("http://localhost:8080")
    # map = {"CARPLATE":"鲁RW798J"}
    map = {"CARPLATE":"鲁RT0355"}
    # map = {"SPEED": "0"}
    mapStr = json.dumps(map)
    # mapStr = ""
    # 读取数据
    # df = client.queryByTime("ehlindex201706", "pass_car", "PASSTIME", "2017-06-07 20:42:35", "2017-06-07 21:47:35",mapStr)
    # print(client.save("ehl201706","pass_car",df))
    # df = client.queryByTime("ehl201706", "pass_car", "PASSTIME", "2017-06-07 20:43:00", "2017-06-07 20:47:00", mapStr)
    r = client.delete("ehlindex201706", "pass_car", "PASSTIME", "2017-06-07 20:42:35", "2017-06-07 21:47:35",mapStr)

    # r = client.delete("ehl201706", "pass_car", "PASSTIME", "2017-06-07 20:44:00", "2017-06-07 20:46:00", mapStr)
    print(r)
    # df = client.queryByTime("ehl201706", "pass_car", "", "", "", mapStr)
    # print(len(df))
    # for i in range(2):
        # df = client.queryByTimes("ehlindex201706", "pass_car", "PASSTIME", "2017-06-07 20:27:35", "2017-06-07 20:47:35",mapStr)
        # df = client.queryByTime("ehlindex201706", "pass_car", "PASSTIME", "2017-06-07 20:27:35", "2017-06-07 20:47:35",mapStr)
        # print(len(df))
        # time.sleep(5)
    # dsf = client.queryByTime("ehlindex201706", "pass_car", "PASSTIME", "2017-06-07 20:27:35", "2017-06-07 20:47:35",mapStr)
    # df['CARBRAND'][0] = '110'
    # sf = df.iloc[:1]
